package com.jwthhealth_pub.old.diet.presenter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.common.preference.PreferenceKey;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.diet.moudel.DietSecModule;
import com.jwthhealth_pub.old.diet.moudel.DietThiModule;
import com.jwthhealth_pub.old.diet.view.adapter.DietSecAdapter;
import com.jwthhealth_pub.old.diet.view.fragment.ConstitutionFragment;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/12/28.
 */

public class IDietPresneterComple implements IDietpresenter {

    private Context mContext;
    private Handler handler;

    public IDietPresneterComple(Context mContext) {
        this.mContext = mContext;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    @Override
    public void requestSecInfo(final String topId, final String id) {
        Call<DietSecModule> call = ApiHelper.dietRequestSecInfo(topId, id);
        Log.d("IDietPresneterComple", "call.request().url():" + call.request().url());
        call.enqueue(new Callback<DietSecModule>() {
            @Override
            public void onResponse(Call<DietSecModule> call, Response<DietSecModule> response) {
                if (response == null) {
                    handler.sendEmptyMessage(ConstitutionFragment.ERROR);
                    return;
                }
                DietSecModule body1 = response.body();
                if (body1 == null) {
                    handler.sendEmptyMessage(ConstitutionFragment.ERROR);
                    return;
                }
                DietSecModule body = body1;
                String code = body.getCode();
                if (code == null) {
                    handler.sendEmptyMessage(ConstitutionFragment.ERROR);
                    return;
                }
                if (code.equals("0")){
                    App.preferenceUtil.putBoolean(PreferenceKey.CACHE_TOKEN, true);
                    Message message = new Message();
                    message.what = ConstitutionFragment.JUMP;
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.TOPID, topId);
                    bundle.putString(Constant.ID, id);
                    bundle.putSerializable(Constant.DIETDATA, body);
                    message.setData(bundle);
                    handler.sendMessage(message);
                } else {
                    List<DietSecModule.DataBean> data = body.getData();
                    Message message = new Message();
                    message.what = ConstitutionFragment.TOAS;
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.DIETDATA, body.getMsg());
                    message.setData(bundle);
                    handler.sendMessage(message);
                }
            }

            @Override
            public void onFailure(Call<DietSecModule> call, Throwable t) {
                Log.d("request url:", "login fail");
            }
        });
    }

    @Override
    public void requestThiInfo(final String topId, final String id, final String sonid, String page, final String typename) {
        Call<DietThiModule> call = ApiHelper.dietRequestThiInfo(topId, id, sonid, page);



        call.enqueue(new Callback<DietThiModule>() {
            @Override
            public void onResponse(Call<DietThiModule> call, Response<DietThiModule> response) {
                Log.d("IDietPresneterComple", response.body().toString());
                DietThiModule module = response.body();
                if (module == null){
                    Message message = new Message();
                    message.what = DietSecAdapter.TOAS;
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.DIETDATA, mContext.getResources().getString(R.string.diet_nodata));
                    message.setData(bundle);
                    Log.d("IDietPresneterComple", "null data");
                    handler.sendMessage(message);
                    return;
                }
                if (module.getCode().equals("0")){
                    Log.d("IDietPresneterComple", "2");
                    Message message = new Message();
                    message.what = DietSecAdapter.JUMP;
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(Constant.DIETDATA, module);
                    bundle.putString(Constant.TYPENAME, typename);
                    bundle.putString(Constant.TOPID, topId);
                    bundle.putString(Constant.ID, id);
                    bundle.putString(Constant.SONID, sonid);
                    message.setData(bundle);
                    handler.sendMessage(message);
                } else {
                    Message message = new Message();
                    message.what = DietSecAdapter.TOAS;
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.DIETDATA, module.getMsg());
                    message.setData(bundle);
                    Log.d("IDietPresneterComple", "fail");
                    handler.sendMessage(message);
                }
            }

            @Override
            public void onFailure(Call<DietThiModule> call, Throwable t) {
            }
        });
    }
}
